package com.gosling.cloudspace.common.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class HuangNiuHandle implements InvocationHandler {

    public interface Human {
        void eat();
        void sleep();
        void lookConcert();
    }

    public static class HuangNiu implements Human{
        private Me me;
        public HuangNiu() {
            me = new Me();
        }
        @Override
        public void eat() {
        }
        @Override
        public void sleep() {
        }
        @Override
        public void lookConcert() {
            // 添加排队买票方法
            this.lineUp();
            me.lookConcert();
        }
        public void lineUp() {
            System.out.println("排队买票");
        }
    }
    public static class Me implements Human{
        @Override
        public void eat() {
        }
        @Override
        public void sleep() {
        }
        @Override
        public void lookConcert() {
        }
    }

    private Object proxyTarget;

    public Object getProxyInstance(Object target) {
        this.proxyTarget = target;
        return Proxy.newProxyInstance(proxyTarget.getClass().getClassLoader(), proxyTarget.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object methodObject = null;
        System.out.println("开始");
        methodObject = method.invoke(proxyTarget, args);
        System.out.println("结束");
        return methodObject;
    }

    public static void main(String[] args) {

        //============静态代理============
        Human human1 = new HuangNiu();
        human1.lookConcert();

        //============动态代理============
        HuangNiuHandle huangNiuHandle = new HuangNiuHandle();
        Human human = (Human) huangNiuHandle.getProxyInstance(new Me());
        human.eat();
        human.sleep();
        human.lookConcert();
        System.out.println("------------------");
//        Animal animal = (Animal) huangNiuHandle.getProxyInstance(new Dog());
//        animal.eat();
//        animal.sleep();
//        animal.seeADoctor();
    }



}